#include "Socket.hpp"

using namespace SocketModule;

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        cout << "server serverport" << endl;
        return 1;
    }
    shared_ptr<Socket> listensock = make_shared<TcpSocket>(stoi(argv[1]));

    listensock->BuildSocket(SockType::LISTEN_SOCK);
    
    listensock->SetBacklog(10);
    int n=10;

    struct sockaddr_in addr;
    shared_ptr<Socket> sock=listensock->Accept(&addr);
    char cip_buf[100];
    string cip;
    uint16_t cport=addr.sin_port;

    inet_ntop(AF_INET,&addr.sin_addr.s_addr,cip_buf,sizeof(cip_buf));

    LOG(LogLevel::DEBUG)<<"成功连接ip："<<cip_buf<<",port: "<<cport;
    while (n--)
    {
        string in;
        string out="你好";

        sock->Recv(in);
        LOG(LogLevel::DEBUG)<<"ip："<<cip_buf<<",port: "<<cport<<"# "<<in;

        sock->Send(out);

        cout<<sock->GetSockfd()<<endl;
        cout<<sock->GetPort()<<endl;
        cout<<sock->GetIp()<<endl;
    }

    sock->Close();

    //---------------------------------------------
    // UDPSocket
    /*shared_ptr<Socket> sock = make_shared<UdpSocket>(stoi(argv[1]));
    sock->SockCreation(SockType::ORDINARY_SOCK);
    sock->Bind();
    sock->Listen();
    sock->SetBacklog(10);

    int n = 10;

    while (n--)
    {
        string in;
        string out = "你好";

        struct sockaddr_in addr;
        sock->Recvfrom(in, &addr);

        char cip_buf[100];
        string cip;
        uint16_t cport = addr.sin_port;
        inet_ntop(AF_INET, &addr.sin_addr.s_addr, cip_buf, sizeof(cip_buf));

        LOG(LogLevel::DEBUG) << "ip：" << cip_buf << ",port: " << cport<<" # "<<in;


        sock->Sendto(out,&addr);

        cout << sock->GetSockfd() << endl;
        cout << sock->GetPort() << endl;
        cout << sock->GetIp() << endl;
    }

    sock->Close();*/

    return 0;
}